# coding: utf-8
from PySide6.QtGui import QColor, Qt, QFontMetrics
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QSizePolicy
from qfluentwidgets import IconWidget, BodyLabel, CaptionLabel, setFont, FluentIcon as FIF, ToolTipFilter, \
    ToolTipPosition, RoundMenu, Action
from qfluentwidgets.components.material import AcrylicMenu

from common import SingleIcon, TranslationDict, Icon, signalBus
from ..widgets import CardPushButton


class TTSCard(CardPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.horizontalLayout_2 = QHBoxLayout()
        self.horizontalLayout_3 = QHBoxLayout()
        self.verticalLayout = QVBoxLayout()

        self.iconWidget = IconWidget(self)
        self.nameLabel = BodyLabel(self)
        self.contentCategoriesIcon = IconWidget(FIF.TAG, self)
        self.contentCategoriesLabel = CaptionLabel(self)
        self.voicePersonalitiesIcon = IconWidget(FIF.CALORIES, self)
        self.voicePersonalitiesLabel = CaptionLabel(self)

        setFont(self.nameLabel, 16)
        self.iconWidget.setFixedSize(50, 50)
        self.contentCategoriesIcon.setFixedSize(14, 14)
        self.voicePersonalitiesIcon.setFixedSize(14, 14)

        self.nameLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        self.contentCategoriesLabel.setTextColor(QColor(0, 0, 0, 200), QColor(255, 255, 255, 200))
        self.voicePersonalitiesLabel.setTextColor(QColor(0, 0, 0, 200), QColor(255, 255, 255, 200))
        self.setFixedHeight(80)

        self.horizontalLayout.setContentsMargins(10, 10, 10, 10)
        self.horizontalLayout.addWidget(self.iconWidget)
        self.verticalLayout.addWidget(self.nameLabel)
        self.horizontalLayout_2.addWidget(self.contentCategoriesIcon)
        self.horizontalLayout_2.addWidget(self.contentCategoriesLabel)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3.addWidget(self.voicePersonalitiesIcon)
        self.horizontalLayout_3.addWidget(self.voicePersonalitiesLabel)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout.addLayout(self.verticalLayout)

        self.installEventFilter(ToolTipFilter(self, position=ToolTipPosition.BOTTOM))
        self.customContextMenuRequested.connect(self.showContextMenu)

    def setUserData(self, data: dict):
        super().setUserData(data)
        locale = data.get('Locale', '')
        voiceTag = data.get('VoiceTag', {})
        name = TranslationDict.get(data.get('FriendlyName', ''), data.get('FriendlyName', ''))
        contentCategories = ', '.join([TranslationDict.get(i, i) for i in voiceTag.get('ContentCategories', [])])
        voicePersonalities = ', '.join([TranslationDict.get(i, i) for i in voiceTag.get('VoicePersonalities', [])])
        newName = QFontMetrics(self.nameLabel.font()).elidedText(name, Qt.TextElideMode.ElideRight,
                                                                 self.width() - 80)
        self.nameLabel.setText(newName)
        gender = data.get('Gender', '')
        if gender == 'Male':
            self.iconWidget.setIcon(SingleIcon.MALE)
        else:
            self.iconWidget.setIcon(SingleIcon.FEMALE)
        self.contentCategoriesLabel.setText(contentCategories)
        self.voicePersonalitiesLabel.setText(voicePersonalities)

        self.setToolTip(f"{name}\n{contentCategories}\n{voicePersonalities}")

    def resizeEvent(self, event):
        name = TranslationDict.get(self.userData.get('FriendlyName'), self.userData.get('FriendlyName'), )
        self.nameLabel.setText(
            QFontMetrics(
                self.nameLabel.font()
            ).elidedText(
                name,
                Qt.TextElideMode.ElideRight,
                self.width() - 80
            )
        )
        super().resizeEvent(event)

    def showContextMenu(self, pos):
        menu = AcrylicMenu(parent=self)
        # 批量转换
        action = Action(Icon.SELECT, '批量转换', menu, triggered=self.triggered)
        menu.addAction(action)
        menu.exec(self.mapToGlobal(pos))

    def triggered(self):
        userData = self.userData
        language = userData.get('Locale', '')
        if 'zh-' in language:
            language = 'zh-CN'

        signalBus.switchToSample.emit('batchInterface', {'language': language, 'shortName': userData['ShortName']})
